package demo_wait;

import bean.WaitAndNotifyBean;

/**
 * @author: android 超级兵
 * @create: 2022-01-15 15:14
 * TODO wait notify notifyAll 的使用
 **/
public class WaitNotifyClient {

    private class MyThread extends Thread {

        private final WaitAndNotifyBean notifyBean;

        public MyThread(WaitAndNotifyBean notifyBean) {
            this.notifyBean = notifyBean;
        }
        @Override
        public void run() {
            super.run();
            // 等待
            notifyBean.waitName(getName());
        }
    }

    private void build() throws InterruptedException {
        WaitAndNotifyBean waitAndNotifyBean = new WaitAndNotifyBean();

        for (int i = 0; i < 5; i++) {
            new MyThread(waitAndNotifyBean).start();
        }

        Thread.sleep(1000);

        System.out.println("我已经等待1s了,你回来了吗?");
        waitAndNotifyBean.changeName();
    }


    public static void main(String[] args) throws InterruptedException {
        // 等待方:
        // 1.获取条件的锁
        // 2.检查条件 条件不满足 wait
        // 2.检查条件 条件不满足 wait
        WaitNotifyClient waitNotifyClient = new WaitNotifyClient();
        waitNotifyClient.build();
    }
}
